﻿
layui.use(function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    var table = layui.table;

    var token = localStorage.getItem("token");
    var selectedRows = [];
    //显示表格
    function Getprocurement(pageNum) {
        var dto = {
            Token: token,
            PageIndex: 1,
            PageSize: 10,
            StockCode: $("#StockCode").val() || '',
            StockOrderType: $("#StockOrderType").val() || '',
            StateSP: $("#StateSP").val() || '',
        };
        // 发送Ajax请求
        $.ajax({
            url: '/EnterStorage/EnterStorageSelect',  // 这里填上你的后端接口地址
            type: 'get',
            data: dto,
            success: function (response) {
                var res = JSON.parse(response);
                console.log(res.data.list);
                if (res != null) {
                    // 渲染表格
                    table.render({
                        elem: '#ID-table',
                        cols: [[
                            { type: 'checkbox', fixed: 'left' },
                            { field: 'stockCode', title: '订单号', width: 150, fixed: 'left', },
                            { field: 'productsName', title: '产品名称', width: 120 },
                            { field: 'productsCode', title: '产品编号', width: 120 },
                            { field: 'batch', title: '批次', width: 120 },
                            { field: 'specs', title: '规格', width: 100 },
                            { field: 'quantity', title: '数量', width: 100 },
                            { field: 'storehouseName', title: '库位', width: 100 },
                            {
                                field: 'stockOrderType', title: '入库类型', width: 120, templet: function (d) {
                                    switch (d.stockOrderType) {
                                        case 1:
                                            return '采购入库';
                                        case 2:
                                            return '生产退货入库';
                                        case 3:
                                            return '销售入库';
                                        case 4:
                                            return '其他入库';
                                    }
                                }
                            },
                            { field: 'vendorName', title: '供应商', width: 100 },
                            { field: 'makePerson', title: '制单人', width: 100 },
                            {
                                field: 'stateSP', title: '状态', width: 100, templet: function (d) {
                                    switch (d.stateSP) {
                                        case 1:
                                            return '<span style="color:orange">待审核</span>';
                                        case 2:
                                            return '<span style="color:green">审核成功</span>';
                                        case 3:
                                            return '<span style="color:red">审核失败</span>';
                                    }
                                }
                            },
                            { field: 'shenHePeople', title: '审核人', width: 100 },
                            { field: 'shenHeTime', title: '审核时间', width: 100 },
                            {
                                field: 'stockProductId',
                                title: '操作',
                                width: 250,
                                templet: function (d) {
                                    // 这里的d是每一行的数据对象
                                    if (d.stateSP == 1) {
                                        return '<a lay-on="test-page" id="detail" data-inProductId="' + d.stockId + '">查看 </a>' +
                                            '<a lay-on="test-del" id="del" data-procurementId="' + d.stockId + '">删除 </a>' +
                                            '<a lay-on="test-fan" class="fan" data-productsID="' + d.stockId + '"  href="/EnterStorage/EnterStorageUpdate?stockId=' + d.stockId + '&procurementId=' + d.stockId + '">编辑 </a>' +
                                            ' <a lay-on="test-ru" id="he" data-movelibraryId="' + d.stockId + '" data-inProductId="' + d.stockId + '">审核 </a>'
                                    } else {
                                        return '<a lay-on="test-page" id="detail" data-inProductId="' + d.stockId + '">查看 </a>' +
                                            '<a lay-on="test-del" id="del" data-procurementId="' + d.stockId + '">删除 </a>' +
                                            '<a lay-on="test-fan" class="fan" data-productsID="' + d.stockId + '"  href="/EnterStorage/EnterStorageUpdate?stockId=' + d.stockId + '&procurementId=' + d.stockId + '">编辑 </a>' +
                                            ' '
                                    }
                                }
                            },
                        ]],
                        data: res.data.list,  // 数据源
                        page: true,   // 启用分页
                        limit: 10,    // 每页显示多少条数据
                        limits: [10, 20, 30],  // 分页选择项
                        curr: pageNum,  // 当前页
                        total: res.data.lcount,  // 总记录数
                        loading: true, // 启用加载状态
                        toolbar: '#toolbarDemo',
                        defaultToolbar: ['exports']
                    });
                    // 渲染分页控件
                    var totalPages = Math.ceil(res.lcount / 10);  // 计算总页数
                    layui.laypage.render({
                        elem: 'pagination',
                        count: res.lcount,  // 总记录数
                        limit: 10,
                        curr: pageNum,
                        jump: function (obj, first) {
                            if (!first) {
                                Getprocurement(obj.curr);  // 不为第一次调用时，重新渲染表格
                            }
                        }
                    });
                }
            },
            error: function (xhr, status, error) {
                console.log('请求失败: ' + error);
            }
        });
    }

    // 初次加载表格数据
    Getprocurement(1);

    // 查询按钮点击事件
    $('#searchBtn').click(function () {
        Getprocurement(1);  // 重置为第一页，并重新加载数据
    });

    // 获取选中数据按钮
    table.on('checkbox(ID-table)', function (obj) {
        // 当前选中的行加入到 selectedRows 数组中
        if (obj.checked) {
            selectedRows.push(obj.data);
        }
        else {
            // 当前取消选中的行从 selectedRows 数组中移除
            selectedRows = selectedRows.filter(row => row.productsID !== obj.data.productsID);
        }
        console.log(selectedRows)
    })

    //删除
    $(document).on('click', '#del', function () {
        var InProductId = $(this).attr('data-procurementId')
        layer.confirm('确认删除吗？', {
            btn: ['确定', '关闭'] //按钮
        }, function () {
            var id = {
                ids: [
                    InProductId
                ],
                delName: '张三'
            }
            id = JSON.stringify(id);
            $.ajax({
                url: '/EnterStorage/EnterStorageDelete',
                type: 'post',
                data: {
                    id: id,
                    Token: token,
                },
                success: function (res) {
                    var d = JSON.parse(res);
                    if (d != null) {
                        layer.msg('删除成功', { icon: 0 }, function () {
                            Getprocurement(1);
                        });
                    }
                    else {
                        alert('删除失败');
                    }
                }
            })

        }, function () {
            // 关闭框第二个回调
            layer.msg('操作已取消', {
                time: 2000 // 2秒后自动关闭
            });
        });
    })
    //查看详情
    $(document).on('click', '#detail', function () {
        var inProductId = $(this).attr('data-inProductId');  // 获取按钮上设置的采购ID
        console.log(inProductId);
        //打开弹框
        var index = layer.open({
            type: 1,
            area: ['800px', '500px'], // 宽高
            content: '<table style="width:650px;height:300px; margin-left:45px">' +
                '<tr>' +
                '<td>入库单号:</td>' +
                '<td id="stockCode"></td>' +
                '<td style="width:100px">入库单类型：</td>' +
                '<td id="stockOrderType" style="width:100px"></td>' +
                '<td style="width:100px">入库状态：</td>' +
                '<td id="sorderNumber"></td>' +
                '</tr>' +
                '<tr>' +
                '<td>供应商编号：</td>' +
                '<td id="vendorId"></td>' +
                '<td>供应商名称：</td>' +
                '<td id="vendorName"></td>' +
                '<td>联系人：</td>' +
                '<td id="vendorContact"></td>' +
                '</tr>' +
                '<tr>' +
                '<td>电话:</td>' +
                '<td id="vendorPhone"></td>' +
                '<td>制单人：</td>' +
                '<td id="makePerson"></td>' +
                '<td>制单日期：</td>' +
                '<td id="markTime"></td>' +
                '</tr>' +
                '<tr>' +
                '<td>备注:</td>' +
                '<td id="sRremark"></td>' +
                '</tr>' +
                '</table>' +
                '<table class= "layui-table" id="vendor-Id" >',

            success: function (lay, index) {
                form.render();
            }
        });
        $.ajax({
            url: '/EnterStorage/EnterStorageSet',
            type: "get",
            data: {
                Token: token,
                id: inProductId
            },
            success: (ress) => {
                var res = JSON.parse(ress);
                $(res.data).each(function () {
                    $("#stockCode").text(this.stockCode);
                    //入库类型是1就是采购入库，2就是生产退货入库，3就是销售入库，4就是其他入库
                    if (this.stockOrderType == 1) {
                        $("#stockOrderType").text("采购入库");
                    } else if (this.stockOrderType == 2) {
                        $("#stockOrderType").text("生产退货入库");
                    } else if (this.stockOrderType == 3) {
                        $("#stockOrderType").text("销售入库");
                    } else if (this.stockOrderType == 4) {
                        $("#stockOrderType").text("其他入库");
                    }
                    $("#sorderNumber").text(this.sorderNumber);
                    $("#vendorId").text(this.vendorId);
                    $("#vendorName").text(this.vendorName);
                    $("#vendorContact").text(this.vendorContact);
                    $("#vendorPhone").text(this.vendorPhone);
                    $("#makePerson").text(this.makePerson);
                    $("#markTime").text(this.markTime.substring(0, 10));
                    $("#sRremark").text(this.sRremark);
                })
                table.render({
                    elem: '#vendor-Id',
                    cols: [[
                        { field: 'productsCode', title: '产品编码', width: 200, fixed: 'left', unresize: true },
                        { field: 'productsName', title: '产品名称', width: 200 },
                        { field: 'specs', title: '规格', width: 150 },
                        { field: 'batch', title: '批次', width: 150 },
                        { field: 'sprice', title: '单价', width: 150 },
                        { field: 'quantity', title: '入库数', width: 150 },
                        { field: 'storehouseName', title: '库位', width: 150 }
                    ]],
                    data: res.data.stockProducts,  // 数据源
                    loading: true, // 启用加载状态
                });
            }
        })
    })
    //批量删除
    $(document).on('click', '#bathdel', function () {
        if (selectedRows.length == 0) {
            layer.msg('至少选择一个要删除的数据');  // '5' 是警告图标
            return;
        }
        var InProductId = selectedRows.map(function (row) {
            return row.stockId;
        });
        layer.confirm('确认删除吗？', {
            btn: ['确定', '关闭'] //按钮
        }, function () {
            var id = {
                ids: InProductId,
                delName: '张三'
            }
            id = JSON.stringify(id);
            $.ajax({
                url: '/EnterStorage/EnterStorageDelete',
                type: 'post',
                data: {
                    id: id,
                    Token: token,
                },
                success: function (res) {
                    var d = JSON.parse(res);
                    if (d != null) {
                        layer.msg('删除成功', { icon: 0 }, function () {
                            Getprocurement(1);
                        });
                    }
                    else {
                        alert('删除失败');
                    }
                }
            })
        }, function () {
            // 关闭框第二个回调
            layer.msg('操作已取消', {
                time: 2000 // 2秒后自动关闭
            });
        });
    });
    $(document).on('click', "#he", function () {
        var movelibraryId = $(this).attr('data-movelibraryId');  // 获取按钮上设置的采购ID
        var inProductId = $(this).attr('data-inProductId');  // 获取按钮上设置的采购ID
        //打开弹框
        var index = layer.open({
            type: 1,
            area: ['800px', '500px'], // 宽高
            content: '<table style="width:650px;height:300px; margin-left:45px">' +
                '<tr>' +
                '<td>入库单号:</td>' +
                '<td id="stockCode"></td>' +
                '<td style="width:100px">入库单类型：</td>' +
                '<td id="stockOrderType" style="width:100px"></td>' +
                '<td style="width:100px">入库状态：</td>' +
                '<td id="sorderNumber"></td>' +
                '</tr>' +
                '<tr>' +
                '<td>供应商编号：</td>' +
                '<td id="vendorId"></td>' +
                '<td>供应商名称：</td>' +
                '<td id="vendorName"></td>' +
                '<td>联系人：</td>' +
                '<td id="vendorContact"></td>' +
                '</tr>' +
                '<tr>' +
                '<td>电话:</td>' +
                '<td id="vendorPhone"></td>' +
                '<td>制单人：</td>' +
                '<td id="makePerson"></td>' +
                '<td>制单日期：</td>' +
                '<td id="markTime"></td>' +
                '</tr>' +
                '<tr>' +
                '<td>备注:</td>' +
                '<td id="sRremark"></td>' +
                '</tr>' +
                '<tr>' +
                '<td>审核原因：</td>' +
                '<td><textarea placeholder="请输入" class="layui-textarea"  id="ReasonForReviews"></textarea></td>' +
                '</tr>' +
                '<table class= "layui-table" id="Movelibrary-sId" >' +
                '<thead>' +
                '<tr>' +
                '<col width="150">' +
                '<col>' +
                '</tr>' +
                '</thead>' +
                '</table>' +
                '<button type="button" class="layui-btn layui-btn-primary "  id="yes">审核通过</button>' +
                '<button type="button" class="layui-btn layui-btn-primary " id="no">审核不通过</button>',

            success: function (lay, index) {
                form.render();
            }
        });

        //反填
        $.ajax({
            url: '/EnterStorage/EnterStorageSet',
            type: "get",
            data: {
                Token: token,
                id: movelibraryId
            },
            success: (ress) => {
                var res = JSON.parse(ress);
                $(res.data).each(function () {
                    $("#stockCode").text(this.stockCode);
                    //入库类型是1就是采购入库，2就是生产退货入库，3就是销售入库，4就是其他入库
                    if (this.stockOrderType == 1) {
                        $("#stockOrderType").text("采购入库");
                    } else if (this.stockOrderType == 2) {
                        $("#stockOrderType").text("生产退货入库");
                    } else if (this.stockOrderType == 3) {
                        $("#stockOrderType").text("销售入库");
                    } else if (this.stockOrderType == 4) {
                        $("#stockOrderType").text("其他入库");
                    }
                    $("#sorderNumber").text(this.sorderNumber);
                    $("#vendorId").text(this.vendorId);
                    $("#vendorName").text(this.vendorName);
                    $("#vendorContact").text(this.vendorContact);
                    $("#vendorPhone").text(this.vendorPhone);
                    $("#makePerson").text(this.makePerson);
                    $("#markTime").text(this.markTime.substring(0, 10));
                    $("#sRremark").text(this.sRremark);
                })
                table.render({
                    elem: '#vendor-Id',
                    cols: [[
                        { field: 'productsCode', title: '产品编码', width: 200, fixed: 'left', unresize: true },
                        { field: 'productsName', title: '产品名称', width: 200 },
                        { field: 'specs', title: '规格', width: 150 },
                        { field: 'batch', title: '批次', width: 150 },
                        { field: 'sprice', title: '单价', width: 150 },
                        { field: 'quantity', title: '入库数', width: 150 },
                        { field: 'storehouseName', title: '库位', width: 150 }
                    ]],
                    data: res.data.stockProducts,  // 数据源
                    loading: true, // 启用加载状态
                });
            }
        })



        //审核通过
        $(document).on('click', "#yes", function () {
            var model = {
                "id": movelibraryId,
                "state": 2,
                "people": "string"
            }
            $.ajax({
                url: '/EnterStorage/EnterStorageUpdateState',
                type: 'post',
                data: {
                    model: JSON.stringify(model),
                    Token: token
                },
                success: function (d) {
                    var res = JSON.parse(d);
                    if (res.data == 1) {
                        layer.msg('审核通过', { icon: 1 }, function () {
                            location.reload();
                        });
                    }
                }
            })
        })

        //审核不通过
        $(document).on('click', "#no", function () {
            var model = {
                "id": movelibraryId,
                "state": 3,
                "people": "string"
            }
            $.ajax({
                url: '/EnterStorage/EnterStorageUpdateState',
                type: 'post',
                data: {
                    model: JSON.stringify(model),
                    Token: token
                },
                success: function (d) {
                    var res = JSON.parse(d);
                    if (res.data == 1) {
                        layer.msg('审核不通过', { icon: 1 }, function () {
                            location.reload();
                        });
                    }
                }
            })
        })


    })

});